<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="bjui-pageContent">
	<ul id="tree2" class="ztree"></ul>
	<script>
		var zTreeObj;
		var setting = {
				check: {
					enable: true,
					chkStyle: "checkbox",
					chkboxType: { "Y": "ps", "N": "ps" }
				}
			};
		var zNodes = ${tree};
		$(document).ready(function() {
			zTreeObj = $.fn.zTree.init($("#tree2"), setting, zNodes);
		});
		function setAuth() {
			var nodes = zTreeObj.transformToArray(zTreeObj.getNodes());
			var modPris = [];
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].check_Child_State > 0 && nodes[i].level == 2) {
					var children = nodes[i].children;
					for (var s = 0; s < children.length; s++) {
						if (children[s].checked) {
							var modPri = {};
							modPri.modCode = nodes[i].target;
							modPri.priCode = children[s].target;
							modPris.push(modPri);
						}
					}
				} 
			}
			var root_url = "<%=request.getContextPath()%>";
			$.ajax({
				type: "POST",
				url: root_url+"/t/role/setAuth.do",
				cache: false,
				dataType: "json",
				data: "roleCode=${roleCode}&json="+JSON.stringify(modPris),
				success: function(msg){
					$("#close").click();
					if(msg.statusCode == '200') {
						$(this).alertmsg('info', '授权成功！');
					} else {
						$(this).alertmsg('info', msg.message);
					}
				}
			});
		}
	</script>
</div>
<div class="bjui-pageFooter">
    <ul>
        <li><button type="button" class="btn-close" data-icon="close" id="close">关闭</button></li>
        <li><button type="button" onClick="setAuth()" class="btn-default" data-icon="save">保存</button></li>
    </ul>
</div>